if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1

quietly tabulate edu_level_b, gen(edu_level_b)
quietly tabulate lit_eval_b, gen(lit_eval_b)
quietly tabulate num_eval_b, gen(num_eval_b)


*Define strata
egen strata=group(gender state_old)

*Generate treatment streams
gen treatment1=0
replace treatment1=1 if treatment_stream==3 
gen treatment2=0
replace treatment2=1 if treatment_stream==4

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate treatment streams
gen treatmentX=0
replace treatmentX=1 if treatment_stream==3

ren edu_level_b  edu_b

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b


*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict 

global base_controls i.edu_b i.treat_edu i.num_eval_b i.treat_num i.lit_eval_b i.treat_lit


***Socioeconomic Survey outcomes ******************************************************************
clear matrix
mat Z = J(10,8,.)

global n 1
local replace replace
local append
foreach var of varlist employment_index consumption_index savings_debt_index bizskills_index psycho_index risk_index trust_index crime_index migration_inde {
	

	reg `var' treatment treatment1 $geo_controls i.strata, cluster(boma_old) 
	
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat Z[$n,1] = `b_t1'
		
	eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls  i.strata ( treatmentX= Z_treat_kcb) , cluster(boma_old) first

		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat Z[$n,2] = `b_t1'
		
		mat Z[$n, 3] =  abs(Z[$n, 1] -  Z[$n, 2])
		
		mat Z[$n, 4] =  Z[$n, 3]*0.4455872
		
	
	
	reg `var' treatment treatment1 $geo_controls $base_controls i.strata, cluster(boma_old) 
	
		local b_t1 = _b[treatment1]
		display `b_t1'
		mat Z[$n,5] = `b_t1'
		
	eststo: xi: ivreg2 `var' lg_kcb_dist treatment $geo_controls $base_controls i.strata ( treatmentX= Z_treat_kcb) , cluster(boma_old) first

		local b_t1 = _b[treatmentX]
		display `b_t1'
		mat Z[$n,6] = `b_t1'
		
		mat Z[$n, 7] =  abs(Z[$n, 5] -  Z[$n, 6])
		
		mat Z[$n, 8] =  Z[$n, 7]*0.4455872
		
		global n = $n + 1
}

mat A = J(9,2,.)
forvalues i = 1/9 {
    matrix A[`i',1] = Z[`i',4]
}
forvalues i = 1/9 {
    matrix A[`i',2] = Z[`i',8]
}
		
		* Make TOT table
		matrix rownames A = "Employment index" "Consumption index" "Savings index" "Business skills index" "Psychological wellbeing index" "Risk index" "Trust index" "Crime and violence index" "Migration index" 
		frmttable using "$gsdTables/TableA.5.tex", tex statmat(A) replace plain sdec(3)  title("Rho-test") note(" ")

	